package com.example.wms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wms.common.QueryPageParam;
import com.example.wms.common.Result;
import com.example.wms.entity.Admin;
import com.example.wms.entity.Grade;
import com.example.wms.entity.Menu;
import com.example.wms.entity.User;
import com.example.wms.service.IAdminService;
import com.example.wms.service.IMenuService;
import com.example.wms.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wms
 * @since 2023-01-15
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @Autowired
    private IMenuService menuService;

    @GetMapping("/list")
    public Result list(){
        return Result.success(adminService.list());
    }

    @PostMapping("/save")
    public Result save(@RequestBody Admin admin){
        System.out.println("user save"+admin);
        return adminService.save(admin)?Result.success():Result.fail();
    }

    @PostMapping("/update")
    public Result saveOrMod(@RequestBody Admin admin){
        return adminService.updateById(admin)?Result.success():Result.fail();
    }

    @GetMapping("/del")
    public Result delete(Integer id){
        return adminService.removeById(id)?Result.success():Result.fail();
    }

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin){
        System.out.println("login"+admin);

        List list = adminService.lambdaQuery()
                .eq(Admin::getAccount,admin.getAccount())
                .eq(Admin::getPassword,admin.getPassword()).list();


        if(list.size()>0){
            Admin admin1 = (Admin)list.get(0);
            List menuList = menuService.lambdaQuery().like(Menu::getMenuright,admin1.getRoleId()).list();
            HashMap res = new HashMap();
            res.put("user",admin1);
            res.put("menu",menuList);
            return Result.success(res);
        }
        return Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        System.out.println("listPage="+query);
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String sex =  (String)param.get("sex");
        System.out.println("name="+name);
        Page<Admin> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper();
        //lambdaQueryWrapper.like(Admin::getAccount,name);

        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Admin::getName,name);
        }
        if(StringUtils.isNotBlank(sex) && !"null".equals(sex)){
            lambdaQueryWrapper.eq(Admin::getSex,sex);
        }

        IPage result = adminService.page(page,lambdaQueryWrapper);
        System.out.println("total=="+result.getTotal());
        return Result.success(result.getRecords(),result.getTotal());
    }
}
